package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.client.Gender;
import com.acme.bankapp.exceptions.ClientExistsException;
import com.acme.bankapp.exceptions.NotEnoughFundsException;

import java.io.*;

public class BankService {

    public static final String fileName = "bank.ser";

    public static void addClient(Bank bank, String name, Gender gender, double amount, double overdraft) throws ClientExistsException {
        bank.addClient(name, gender, amount, overdraft);
    }

    public static void modifyClient(Bank bank, String clientName, double transactionAmount) throws NotEnoughFundsException,IOException {
        bank.modifyClient(clientName, transactionAmount);
        saveBank(bank, new File(fileName));
    }

    public static double getBalance(Bank bank, String clientName) {
        return bank.getBalance(clientName);
    }

    public static void printMaximumAccountToWithDraw(Bank bank) {
        System.out.println("Maximum Account To With Draw:");
        bank.printMaximumAccountToWithDraw();
    }

    public static void dumpToFile(Bank bank, File file) throws IOException{
        bank.dumpToFile(file);
    }

    public static void saveBank(Bank bank, File file) throws IOException{
        try (ObjectOutputStream bos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)))){
            bos.writeObject(bank);
        }
    }
    public static Bank readBank(File file) throws IOException,ClassNotFoundException{
        try (ObjectInputStream bis = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)))){
            Bank bank = (Bank)bis.readObject();
            bank.initBankSettings();
            return bank;
        }
    }
}
